/** 
*
* @author  jiwf 
* @date 创建时间：2016年10月13日 下午10:03:53 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xbstar.bean.AffixInfo;
import com.xbstar.bean.DestinationInfor;
import com.xbstar.dao.AffixInfoRepository;
import com.xbstar.dao.DestinationRepository;
import com.xbstar.tools.BeanUtils;

@RestController
@RequestMapping("/dest")
@CrossOrigin
public class DestController {
	
	@Autowired
	private DestinationRepository destRe;
	@Autowired
	private AffixInfoRepository affixRe;
	@RequestMapping("/add")
	public String addDest(HttpServletRequest req){
		JSONObject json=new JSONObject();
		DestinationInfor dest=new DestinationInfor();
		String cityName=req.getParameter("cityName");
		String country=req.getParameter("country");
		String affixBelongTo=req.getParameter("affixBelongTo");
		String description=req.getParameter("description");
		String index=req.getParameter("index");
		String province=req.getParameter("province");
		dest.setCityName(cityName);
		dest.setAffixBelongTo(affixBelongTo);
		dest.setDescription(description);
		dest.setProvince(province);
		if(index==null)index="0";
		dest.setIndexFor(index);
		dest.setCountry(country);
		//1.按照cityname进行查找
		//2.判断是否存在
		//3,。若存在则显示失败
		//4.不存在则加入
		List<DestinationInfor> result=null;
		try {
			result = destRe.findByCityNameLike(cityName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result!=null&&result.size()>0){
			json.put(UserPackage.STATUS, UserPackage.REDO);
		}else{
			try {
				destRe.save(dest);
				json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			} catch (Exception e) {
				json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
				e.printStackTrace();
			}
		}	
		return json.toString();
		
	}
	@RequestMapping("/all")
	public String finAllDest(){
		JSONObject json=new JSONObject();
		List<DestinationInfor> result=null;
		try {
			result = (List<DestinationInfor>) destRe.findAllOrderbyIndexAndId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result==null||result.size()==0){
			json.put(UserPackage.STATUS, UserPackage.EMPTY);
		}else{
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			JSONArray array=new JSONArray();
			for (DestinationInfor destinationInfo : result) {
				JSONObject tjson=destinationInfo.getJSON();
				if(destinationInfo.getAffixBelongTo()!=null){
					List<AffixInfo> affixArr=null;
					try {
						affixArr = affixRe.findALLByBelongTo(destinationInfo.getAffixBelongTo());
					} catch (Exception e) {
						e.printStackTrace();
					}
					JSONArray picarr=new JSONArray();
					if(affixArr!=null&&affixArr.size()>0){
						
						for (AffixInfo affixInfo : affixArr) {
							picarr.put(affixInfo.getJSON());
						}
						
					}
					tjson.put("pics", picarr);	
				}
				array.put(tjson);
			}
			json.put("data", array);
		}
		return json.toString();
	}
	@RequestMapping("/edit")
	public String editDest(@RequestParam("id") Long id,HttpServletRequest req){

		DestinationInfor destinfo=null;
		JSONObject json=new JSONObject();
		try {
			destinfo = destRe.findOne(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(destinfo==null){
			json.put(UserPackage.STATUS, UserPackage.EMPTY);
		}else{
			DestinationInfor destnew=new DestinationInfor();
			destnew.setId(id);
			destnew.setAffixBelongTo(req.getParameter("affixBelongTo"));
			destnew.setCityName(req.getParameter("cityName"));
			destnew.setCountry(req.getParameter("country"));
			destnew.setDescription(req.getParameter("description"));
			destnew.setIndexFor(req.getParameter("indexFor"));
			destnew.setProvince(req.getParameter("province"));
			try {
				destnew=BeanUtils.merge(destinfo, destnew);
				destRe.save(destnew);
				json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
			}
		}
		return json.toString();
	}
	/**
	 * 删除目的地
	 * @param id
	 * @return
	 */
	@RequestMapping("/del")
	public String editDest(@RequestParam("id") Long id){
		JSONObject json=new JSONObject();
		try {
			destRe.delete(id);
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}
		return json.toString();
	}
	/**
	 * 获得目的地下拉框时候用
	 * @return
	 */
	@RequestMapping("/maindest")
	public String getMainDest(){
		JSONObject json=new JSONObject();
		List<DestinationInfor> result=null;
		try {
			result = (List<DestinationInfor>) destRe.findAllOrderbyIndexAndId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result==null||result.size()==0){
			json.put(UserPackage.STATUS, UserPackage.EMPTY);
		}else{
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			JSONArray array=new JSONArray();
			result.stream().forEach(item -> {
				JSONObject jsoq=new JSONObject();
				jsoq.put("id", item.getId());
				jsoq.put("cityName", item.getCityName());
				jsoq.put("country", item.getCountry());
				jsoq.put("province", item.getProvince());
				array.put(jsoq);
			});
			json.put(UserPackage.DATA, array);
		}
		return json.toString();
	}
}
